﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Web;
using ChapterX.Common;

namespace ChapterX.BusinessFacade
{
    public class UploadSystem
    {
        public static String GetIngestPath(HttpContext context)
        {
            String ingestPath = null;
            String pagePath = context.Request.UrlReferrer.AbsolutePath;
            NameValueCollection queryString = HttpUtility.ParseQueryString(context.Request.UrlReferrer.Query);

            if (pagePath.EndsWith("CourseEntry.aspx"))
            {
                ingestPath = context.Server.MapPath(String.Format("{0}\\{1}\\", 
                    ChapterXConfiguration.CourseAttachmentsDir, queryString[QueryKeys.COURSE_ID]));
            }
            else if (pagePath.EndsWith("BookEntry.aspx"))
            {
                ingestPath = context.Server.MapPath(String.Format("{0}\\{1}\\",
                    ChapterXConfiguration.BookAttachmentsDir, queryString[QueryKeys.BOOK_ID]));
            }
            else if (pagePath.EndsWith("ChapterEntry.aspx"))
            {
                ingestPath = context.Server.MapPath(String.Format("{0}\\{1}\\",
                    ChapterXConfiguration.BookChaptersDir, queryString[QueryKeys.BOOK_ID]));
            }
            else
            {
                throw new UnexpectedException();
            }

            if (!Directory.Exists(ingestPath))
            {
                Directory.CreateDirectory(ingestPath);
            }

            return ingestPath;
        }
    }
}
